package com.csr.csrcheck.service.impl;

import com.csr.csrcheck.mapper.ShareholderMapper;
import com.csr.csrcheck.pojo.Shareholder;
import com.csr.csrcheck.service.ShareholderService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShareholderServiceImpl implements ShareholderService {
    @Resource
    private ShareholderMapper shareholderMapper;

    public PageResult getListPage(int pageNum, int pageSize, String shareholder_name, String share_type, String company_name) {
        return PageUtils.getPageResult(pageNum, pageSize, getPageInfo(pageNum, pageSize, shareholder_name, share_type, company_name));
    }

    public void addShareholder(Shareholder shareholder) {
        int row = this.shareholderMapper.addShareholder(shareholder);
        if (row != 1)
            throw new CompanyException("");
    }

    public List<Shareholder> list() {
        List<Shareholder> list = this.shareholderMapper.list();
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    public int updateShareholder(Shareholder shareholder) {
        int row = this.shareholderMapper.updateShareholder(shareholder);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public Shareholder findshareholderByid(int id) {
        Shareholder list = this.shareholderMapper.findshareholderByid(id);
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    public int deleteshareholderByid(int id) {
        int row = this.shareholderMapper.deleteshareholderByid(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    private PageInfo<Shareholder> getPageInfo(int pageNum, int pageSize, String shareholder_name, String share_type, String company_name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Shareholder> list = this.shareholderMapper.getListPage(shareholder_name, share_type, company_name);
        if (list == null)
            throw new CompanyException("");
        return new PageInfo(list);
    }
}
